﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// GameObject 类扩展
/// </summary>
public static class GameObjectExtension
{

    /// <summary>
    /// 获取或者添加默认的 Text 组件
    /// </summary>
    /// <param name="gameObject"></param>
    /// <returns></returns>
    public static Text GetOrAddDefaultText(this GameObject gameObject)
    {
        Text text = gameObject.GetOrAddGameComponent<Text>();
        //TODO:设置默认字体
        return text;
    }

    /// <summary>
    /// 具体游戏用的 GetComponent
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="gameObject"></param>
    /// <returns></returns>
    public static T GetGameComponent<T>(this GameObject gameObject) where T :Component
    {
        return Utility.Component.GetGameComponent<T>(gameObject);
    }

    /// <summary>
    /// 具体游戏用的 GetComponent
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="gameObject"></param>
    /// <returns></returns>
    public static T GetHotfixComponent<T>(this GameObject gameObject) where T : Component
    {
        return Utility.Component.GetGameComponent<T>(gameObject);
    }

    /// <summary>
    /// 具体游戏用的 GetComponentsInChildren
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="transform"></param>
    /// <returns></returns>
    public static T[] GetGameComponentsInChildren<T>(this GameObject gameObject) where T : Component
    {
        return Utility.Component.GetGameComponentsInChildren<T>(gameObject);
    }

    /// <summary>
    /// 获取子物体中对应名字的物体
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="gameObject"></param>
    /// <param name="name"></param>
    /// <returns></returns>
    public static T GetChildGameComponentByName<T>(this GameObject gameObject, string name) where T : UnityEngine.Component
    {
        return Utility.Component.GetChildGameComponentByName<T>(gameObject.transform, name);
    }

    /// <summary>
    /// 具体游戏用的 GetGameComponentInParent
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="gameObject"></param>
    /// <returns></returns>
    public static T GetGameComponentInParent<T>(this GameObject gameObject) where T : Component
    {
        return Utility.Component.GetGameComponentInParent<T>(gameObject);
    }

    /// <summary>
    /// 具体游戏用的 AddComponent
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="gameObject"></param>
    /// <returns></returns>
    public static T AddGameComponent<T>(this GameObject gameObject) where T : Component
    {
        return Utility.Component.AddGameComponent<T>(gameObject);
    }

    /// <summary>
    /// 查找 GameObject 下指定路径的组件
    /// </summary>
    /// <typeparam name="T">组件类型</typeparam>
    /// <param name="gameObject"></param>
    /// <param name="path"></param>
    /// <returns></returns>
    public static T FindComponent<T>(this GameObject gameObject, string path) where T : Component
    {
        return Utility.Component.FindGameComponent<T>(gameObject, path);
    }

    /// <summary>
    /// 获取或增加组件。
    /// </summary>
    /// <typeparam name="T">要获取或增加的组件。</typeparam>
    /// <param name="gameObject">目标对象。</param>
    /// <returns>获取或增加的组件。</returns>
    public static T GetOrAddGameComponent<T>(this GameObject gameObject) where T : Component
    {
        var isHotType = (typeof(T).BaseType == typeof(GameMonoBehaviour));
        T component = isHotType ? gameObject.GetGameComponent<T>() : gameObject.GetComponent<T>();
        if (component == null)
        {
            component = isHotType ? gameObject.AddGameComponent<T>() : gameObject.AddComponent<T>();
        }

        return component;
    }

    /// <summary>
    /// 移除组件
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="gameObject"></param>
    public static void RemoveComponent<T>(this GameObject gameObject, bool isImmediate = false) where T : Component
    {
        if (isImmediate)
        {
            Object.DestroyImmediate(gameObject.GetComponent<T>());
        }
        else
        {
            Object.Destroy(gameObject.GetComponent<T>());
        }
    }

    public static List<GameObject> GetChildsByTag(this GameObject gameObject, string tag)
    {
      
        return null;
    }

    public static void ReActive(this GameObject go)
    {
        go.SetActive(false);
        go.SetActive(true);
    }

    public static void SetPsFreeze(this GameObject obj, bool isFreeze)
    {
        ParticleSystem[] pss = obj.GetComponentsInChildren<ParticleSystem>();
        if (isFreeze)
        {
            for (int i = 0; i < pss.Length; i++)
            {
                pss[i].Pause();
            }
        }
        else
        {
            for (int i = 0; i < pss.Length; i++)
            {
                pss[i].Play();
            }
        }
    }
    
    public static void SetGray(this GameObject btn, bool isGray)
    {
        var imgs = btn.GetComponentsInChildren<Image>(true);
        for (int i = 0, cnt = imgs.Length; i < cnt; i++)
        {
            imgs[i].SetGray(isGray);
        }

        var texts = btn.GetComponentsInChildren<Text>(true);
        for (int i = 0, cnt = texts.Length; i < cnt; i++)
        {
            texts[i].SetGray(isGray);
        }

        var pss = btn.GetComponentsInChildren<ParticleSystem>(true);
        for (int i = 0, cnt = pss.Length; i < cnt; i++)
        {
            pss[i].gameObject.SetActive(!isGray);
        }
    }

    public static GameObject UpdateNewer(this GameObject go, GameObject newer)
    {
        if (go != null && go)
        {
            GameObject.Destroy(go);
        }
        return newer;
    }
}
